package xyz.yeidi.dao.impl;

import xyz.yeidi.dao.api.UserDao;
import xyz.yeidi.pojo.User;
import xyz.yeidi.utils.ConnUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author JIP
 * @Date 2021-07-29 10:03
 * @Version 1.0
 * @Describe : UserDao的实现类
 */
public class UserDaoImpl implements UserDao {
    @Override
    public String getUserByName(String name) {
        String sql = "select userName from user where userName = ?";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,name);
            rs = ps.executeQuery();
            if(rs.next()){
                return rs.getString(1);
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtils.close(rs,ps);
            ConnUtils.close();
        }
        return null;
    }

    @Override
    public User getUserById(Integer id) {
        String sql = "select userId,userName,password,userSex,userImg from user where userId=?";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,id);
            rs = ps.executeQuery();
            //如果存在下一个才new User 否则会报空指针异常
            if(rs.next()){
                return new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getString(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtils.close(rs,ps);
            ConnUtils.close();
        }
        return null;
    }

    @Override
    public User getUserByNameAndPassword(String name, String password) {
        String sql = "select userId,userName,password,userSex,userImg from user where userName=? and password = ?";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,name);
            ps.setObject(2,password);
            rs = ps.executeQuery();
            if(rs.next()){
                User user = new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4),rs.getString(5));
                System.out.println(user);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtils.close(rs,ps);
            ConnUtils.close();
        }
        return null;
    }

    @Override
    public Integer addUser(User user) {
        String sql = "insert into user(password, userName, userSex,userImg) values (?,?,?,?)";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,user.getPassword());
            ps.setObject(2,user.getUserName());
            ps.setObject(3,user.getUserSex());
            ps.setObject(4,user.getUserImg());
            return ps.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnUtils.close(ps);
            ConnUtils.close();
        }
        return 0;
    }
}
